﻿using Fishing.Abstractions;
using Fishing.Common;
using Fishing.Core;
using System.Windows.Input;

namespace Fishing.ViewModel
{
    public class SettingsViewModel : ObservableObject
    {
        private readonly ISettingsService _settingsService;
        private readonly ITaskbarIconService _taskbarIconService;

        public SettingsViewModel(ISettingsService settingsService, ITaskbarIconService taskbarIconService)
        {
            _settingsService = settingsService;
            _taskbarIconService = taskbarIconService;

            IsStartup = WindowsStartup.CheckStartupRegistry() == 1 || WindowsStartup.CheckStartupRegistry() == -1;
            TaskbarIconVisible = _settingsService.Settings.TaskbarIconVisible;
            SelectedIncreaseModeIndex = (int)_settingsService.Settings.IncreaseMode;
            SelectedIncreaseAlgorithmIndex = (int)_settingsService.Settings.IncreaseAlgorithm;
            QuickUpdateKeyBinding = _settingsService.Settings.QuickUpdateKeyBinding;
            ForceQuitKeyBinding = _settingsService.Settings.ForceQuitKeyBinding;
            ExhaleKeyBinding = _settingsService.Settings.ExhaleKeyBinding;
        }

        #region Properties
        private bool _isStartup;
        public bool IsStartup
        {
            get => _isStartup;
            set
            {
                _isStartup = value;
                _settingsService.Settings.Startup = _isStartup;
                OnPropertyChanged();

                WindowsStartup.SetStartupRegistry(_isStartup);
                UpdateConfigFile();
            }
        }

        private bool _taskbarIconVisible;
        public bool TaskbarIconVisible
        {
            get => _taskbarIconVisible;
            set
            {
                _taskbarIconVisible = value;
                _settingsService.Settings.TaskbarIconVisible = _taskbarIconVisible;
                OnPropertyChanged();

                _taskbarIconService.SetVisibitly(_taskbarIconVisible ? System.Windows.Visibility.Visible : System.Windows.Visibility.Hidden);
                UpdateConfigFile();
            }
        }

        private int _selectedIncreaseModeIndex;
        public int SelectedIncreaseModeIndex
        {
            get => _selectedIncreaseModeIndex;
            set
            {
                _selectedIncreaseModeIndex = value;
                OnPropertyChanged();

                if (_settingsService.Settings.IncreaseMode != (IncreaseMode)_selectedIncreaseModeIndex)
                {
                    _settingsService.Settings.IncreaseMode = (IncreaseMode)_selectedIncreaseModeIndex;
                    UpdateConfigFile();
                }
            }
        }

        private int _selectedIncreaseAlgorithmIndex;
        public int SelectedIncreaseAlgorithmIndex
        {
            get => _selectedIncreaseAlgorithmIndex;
            set
            {
                _selectedIncreaseAlgorithmIndex = value;
                OnPropertyChanged();

                if (_settingsService.Settings.IncreaseAlgorithm != (IncreaseAlgorithm)_selectedIncreaseAlgorithmIndex)
                {
                    _settingsService.Settings.IncreaseAlgorithm = (IncreaseAlgorithm)_selectedIncreaseAlgorithmIndex;
                    UpdateConfigFile();
                }
            }
        }

        private KeyGesture _quickUpdateKeyBinding;
        public KeyGesture QuickUpdateKeyBinding
        {
            get => _quickUpdateKeyBinding;
            set
            {
                _quickUpdateKeyBinding = value;
                OnPropertyChanged();

                _settingsService.Settings.QuickUpdateKeyBinding = _quickUpdateKeyBinding;

                UpdateConfigFile();
            }
        }

        private KeyGesture _forceQuitKeyBinding;
        public KeyGesture ForceQuitKeyBinding
        {
            get => _forceQuitKeyBinding;
            set
            {
                _forceQuitKeyBinding = value;
                OnPropertyChanged();

                _settingsService.Settings.ForceQuitKeyBinding = _forceQuitKeyBinding;

                UpdateConfigFile();
            }
        }

        private KeyGesture _exhaleKeyBinding;
        public KeyGesture ExhaleKeyBinding
        {
            get => _exhaleKeyBinding;
            set
            {
                _exhaleKeyBinding = value;
                OnPropertyChanged();

                _settingsService.Settings.ExhaleKeyBinding = _exhaleKeyBinding;

                UpdateConfigFile();
            }
        }
        #endregion

        private void UpdateConfigFile()
        {
            _settingsService.Save();
        }
    }
}